import { useState } from "react";
import Cookies from "js-cookie";
import {isNotEmpty} from "../utils/types.js";
import {Toast} from "@douyinfe/semi-ui";

export const CookieToken = "authorized-token";

export const useZnAdmin = () => {
  const [znAdminToken, setZnAdminToken] = useState("");
  const [znAdminTokenValue, setZnAdminTokenValue] = useState("");

  const initZnAdminToken = () => {
    return new Promise((resolve) => {
      const auth = Cookies.get(CookieToken);
      if (isNotEmpty(auth)) {
        console.log('获取到权限Token', auth)
        resolve(auth)
      } else {
        Toast.error({
          content: '没有访问权限！',
          duration: 3,
        })
        setTimeout(() => {
          window.location.href = 'https://www.zznzzn.xin'
        }, 3000)
        resolve(null)
      }
    })
  }

  return {znAdminToken, znAdminTokenValue, initZnAdminToken};
}
